package cn.touna.jss.modules.custom.loan;

import cn.touna.jss.common.cache.HbaseComponent;
import cn.touna.jss.common.enums.IntfScopeEnum;
import cn.touna.jss.common.factory.ResponseFactory;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.custom.loan.entity.LoanEntity;
import cn.touna.jss.modules.custom.loan.enums.LoanEnum;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.risk.api.utils.JSONUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 多头借贷服务
 * Created by wuwc
 */
@Service
public class LoanService {
    private static final Logger logger = LogManager.getLogger(LoanService.class);

    @Autowired
    private HbaseComponent hbaseComponent;

    /**
     * 多头借贷服务
     * @param loanEntity
     * @return
     */
    @ApiMapping(value = "loan.loanData",desc = "内部数据.查询多头借贷",scope = IntfScopeEnum.JSS_INTF_SYSTEM)
    public Response loanData(LoanEntity loanEntity) {
        Response response = new Response();
        ValidUtil.checkRequestParams(loanEntity);
        try {
            StringBuilder paramsBuilder = new StringBuilder();
            paramsBuilder.append(loanEntity.getIdCard().trim());
            String result = hbaseComponent.queryUserVariables(paramsBuilder.reverse().toString());
            if (StringUtils.isBlank(result)) {
                return response.fail(LoanEnum.LOAN_DATA_ERROR);
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("YiX_Apt_NumOD", JSONUtil.getString(result, "YiX_Apt_NumOD", null));
            resultMap.put("YiX_Apt_NumLoan", JSONUtil.getString(result, "YiX_Apt_NumLoan", null));
            resultMap.put("YiX_Apt_TotalODLoanAmt", JSONUtil.getString(result, "YiX_Apt_TotalODLoanAmt", null));
            resultMap.put("YiX_Loan_Num", JSONUtil.getString(result, "YiX_Loan_Num", null));
            resultMap.put("YiX_OverdueTotal_Array", JSONUtil.getString(result, "YiX_OverdueTotal_Array", null));
            resultMap.put("ZiX_AptPBOC_TotalLoanAmt", JSONUtil.getString(result, "ZiX_AptPBOC_TotalLoanAmt", null));
            resultMap.put("ZiX_AptPBOC_TotalODBal", JSONUtil.getString(result, "ZiX_AptPBOC_TotalODBal", null));
            resultMap.put("ZiX_Repay_LoanNum", JSONUtil.getString(result, "ZiX_Repay_LoanNum", null));
            resultMap.put("ZiX_AptPBOC_TotalLoanBal", JSONUtil.getString(result, "ZiX_AptPBOC_TotalLoanBal", null));
            resultMap.put("ZiX_Spical_12Months_Low_Num", JSONUtil.getString(result, "ZiX_Spical_12Months_Low_Num", null));
            return response.success(SystemErrorCodeEnum.SYSTEM_REQUEST_SUCC, JSON.toJSONString(resultMap));
        } catch (Exception e) {
            logger.error("REE 【查询多头借贷数据】异常,params:{}，msg:{}", loanEntity, e.getMessage());
            return ResponseFactory.fail(SystemErrorCodeEnum.JSS_SYSTEM_ERROR, e.getMessage());
        }
    }


}
